package com.st.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.st.dao.IEmpDao;
import com.st.entity.Emp;
import com.st.util.DBUtil;

public class EmpDaoImpl implements IEmpDao{

	private DBUtil dbUtil = new DBUtil();

	@Override
	public int update(Emp emp, Integer id) {
		return 0;
	}

	@Override
	public int remove(int id) {
		String sql = "delete from emp where eid=?";
		Object[] params = {id};
		return dbUtil.update(sql, params);
	}

	@Override
	public List select(Emp emp) {
		String sql = "select emp.*, dept.dname from emp, dept where emp.did=dept.did ";
		List params = new ArrayList<>();
		
		if (emp.getEname() !=null && emp.getEname().length()>0) {
			sql += " and emp.ename like ? ";  // %%
			params.add("%"+emp.getEname()+"%"); // %null%
		}
		
		if (emp.getEphone() !=null && emp.getEphone().length()>0) {
			sql += " and emp.ephone like ? "; // %%
			params.add("%"+emp.getEphone()+"%");
		}
		
		if (emp.getDid() != null) {
			sql += " and emp.did = ?";			// did=null
			params.add(emp.getDid());
		}
		
		return dbUtil.query(sql, params.toArray());
	}

	@Override
	public int insert(Emp emp) {
		String sql = "insert into emp (ename, ephone, ebirthday, ephoto, did) "
				+ "value (?, ?, ?, ?, ?)";
		Object[] params = {
				emp.getEname(), 
				emp.getEphone(),
				emp.getEbirthday(),
				emp.getEphoto(),
				emp.getDid()
		};
		return dbUtil.update(sql, params);
	}

}
